/**
 * 小柯奇思妙想 2016-11-17 17:21:05
 */

/*
 * 分页 
 * pageNum 页码
 */
function pageHelper(pageNum){
	if(pageNum){
		$("#pageNum").val(pageNum);
	}
	
	$(".ui.dimmer").addClass("active");
	$.ajax({
		url: full_path + "v/category/manage.jhtml",
		type: 'post',
		data: $("#pageHelperForm").serialize(),
		dataType: 'html',
		success: function(data){
			$(".active.segment").empty();
			$(".active.segment").append(data);
			$(".ui.dimmer").removeClass("active");
		}
	});
}

//定义文章分类的验证规则
var rules = {
		name : {
			identifier : 'name',
			rules : [ {
				type : 'empty',
				prompt : '请填写分类名称'
			} ]
		},
		orderIndex: {
			identifier: 'orderIndex',
			rules: [{
				type: 'integer[1..50]',
				prompt: '请输入1-50之间任意整数'
			}]
		}
	};


$(function(){
    //验证新增category的合法性
    $("#add-category-form").form({
    	on: 'blur',
    	fields: rules,
    	inline: true,
    	onValid: function(){
    		//验证分类的唯一性
    		var name = $(this).attr("name");
    		if(name == 'name'){
    			ajaxValidate(name,$(this).val(),$("#add-category-form"));
    		}
    	}
    });	
    
    //验证修改category的合法性
    $("#edit-category-form").form({
    	on: 'blur',
    	fields: rules,
    	inline: true,
    	onValid: function(){
    		//验证分类的唯一性
    		var name = $(this).attr("name");
    		if(name == 'name'){
    			ajaxValidate(name,$(this).val(),$("#edit-category-form"));
    		}
    	}
    });	
});

//弹出新增category框
$("#add-category-modal").modal("attach events","#add-category");

//验证分类的唯一性
function ajaxValidate(selector,value,formObj){
	var id = $(formObj).find('input[name=id]').val();
	var flag = false;
	$.ajax({
		url: 'category/unique.jhtml',
		type: 'post',
		data: {name: selector,value: value,id: id},
		dataType: 'json',
		async: false,
		success: function(data){
            if (data['code'] == "1") {
                $("input[name=" + selector + "]").next().css("visibility", "visible");
                $(formObj).form("remove prompt", selector, data['msg']);
                flag = true;
            } else {
                $("input[name=" + selector + "]").next().css("visibility", "hidden");
                $(formObj).form("add prompt", selector, data['msg']);
                flag = false;
            }
		}
	});
	return flag;
}


//绑定新增category的提交
$("#add-category-primary").click(function(){
	var flag = false;
	$(this).addClass('loading');
	
	//验证表单规则
	flag = $("#add-category-form").form({
        on: 'blur',
        fields: rules,
        inline: true
    }).form("is valid");
 
	 //验证不通过
	 if(!flag){
	     $("#add-category-message").addClass("visible").text("填写文章分类有误");
		 return false;
	 }
	
	//再次验证唯一性
	if(!ajaxValidate('name',$("input[name=name]").val(),$("#add-category-form"))){
		return false;
	}
	 
	 //新增提交
	 $.ajax({
		 url: 'category/insert.jhtml',
		 type: 'post',
		 data: $("#add-category-form").serialize(),
		 dataType: 'json',
		 success: function(data){
			 if(data['code'] == '1'){
				 $("#add-category-primary").removeClass('loading');
				 $("#add-category-modal").modal("hide");
				 layer.msg(data['msg'],{time: 1000});
				 pageHelper(1);
			 }else{
				 $("#add-category-primary").removeClass('loading');
				 $("#add-category-message").addClass("visible").text(data['msg']);
			 }
		 }
	 });
});

//批量删除
$("#delete-category").click(function(){
	var $childCheckbox = $("tbody").find("input[type=checkbox]:checked");
	if(!$childCheckbox || !$childCheckbox.length){
		$(this).popup({on: 'click'}).popup("show");
		return false;
	}else{
		$(this).popup("hide");
	}
});


//编辑分类
function editCategory(id){
	$.ajax({
		url: 'category/edit.jhtml?id=' + id,
		type: 'post',
		dataType: 'json',
		success: function(data){
			if(data['code'] == '1'){
				$("#edit-category-form input[name=id]").val(data['data'].id);
				$("#edit-category-form input[name=name]").val(data['data'].name);
				$("#edit-category-form input[name=content]").val(data['data'].content);
				$("#edit-category-form input[name=orderIndex]").val(data['data'].orderIndex);
				$("#edit-category-form select[name=isPublic] option[value=" + data['data'].isPublic + "]").prop('selected',true);
				$("#edit-category-modal").modal('show');
			}else{
				layer.msg(data['msg'],{time: 1000});
			}
		}
	})
}


//绑定修改;category的提交
$("#edit-category-primary").click(function(){
	var flag = false;
	$(this).addClass('loading');
	
	//验证表单规则
	flag = $("#edit-category-form").form({
        on: 'blur',
        fields: rules,
        inline: true
    }).form("is valid");
 
	 //验证不通过
	 if(!flag){
	     $("#edit-category-message").addClass("visible").text("填写文章分类有误");
		 return false;
	 }
	
	//再次验证唯一性
	if(!ajaxValidate('name',$("input[name=name]").val(),$("#edit-category-form"))){
		return false;
	}
	 
	 //修改分类提交
	 $.ajax({
		 url: 'category/update.jhtml',
		 type: 'post',
		 data: $("#edit-category-form").serialize(),
		 dataType: 'json',
		 success: function(data){
			 if(data['code'] == '1'){
				 $("#edit-category-primary").removeClass('loading');
				 $("#edit-category-modal").modal("hide");
				 layer.msg(data['msg'],{time: 1000});
				 pageHelper(1);
			 }else{
				 $("#edit-category-primary").removeClass('loading');
				 $("#edit-category-message").addClass("visible").text(data['msg']);
			 }
		 }
	 });
});


//删除文章分类
function deleteCategory(id){
	$('#confirm-delete-modal')
	  .modal({
	    closable  : false,
	    onApprove : function() {
	    	$.ajax({
	    		url: 'category/delete.jhtml?id='+id,
	    		type: 'post',
	    		dataType: 'json',
	    		success: function(data){
	    			if(data['code'] == '1'){
	    				 layer.msg(data['msg'],{time: 1500});
	    				 pageHelper();
	    			}else{
	    				 layer.msg(data['msg'],{time: 1500});
	    			}
	    		}
	    	});
	    }
	  }).modal('show');
}

//改变默认状态
function changeDefatul(id,isDefault){
	$.ajax({
		url: "category/change_default.jhtml?id=" + id + "&isDefault=" + isDefault,
		type: 'post',
		dataType: 'json',
		success: function(data){
			if(data['code'] == '1'){
				 layer.msg(data['msg'],{time: 100});
				 pageHelper();
			}else{
				 layer.msg(data['msg'],{time: 1000});
			}
		}
	})
}



